#include "dateobtenu.h"
////////////////////////
//
//  Constructeur
//
////////////////////////
DateObtenu::DateObtenu()
{
    m_id = 0;
    m_clientId = 0;
    m_jourConcert = QDate();
    m_tarifHT = 0.0;
    m_TVA = 0.0;
//    m_typePrestation = typePrestation;
    m_isRepasCompris = false;
    m_isHebergementCompris = false;
    m_isFraisDeplacementCompris = false;
}
DateObtenu::DateObtenu(QDate jourConcert, double tarifHT, double TVA, TypePrestation typePrestation, bool isRepasCompris, bool isHebergementCompris, bool isFraisDeplacementCompris)
{
    m_id = 0;
    m_clientId = 0;
    m_jourConcert = jourConcert;
    m_tarifHT = tarifHT;
    m_TVA = TVA;
    m_typePrestation = typePrestation;
    m_isRepasCompris = isRepasCompris;
    m_isHebergementCompris = isHebergementCompris;
    m_isFraisDeplacementCompris = isFraisDeplacementCompris;
}
DateObtenu::DateObtenu(QSqlQuery query)
{
    m_id = query.value(0).toInt();
    m_clientId = query.value(1).toInt();
    m_jourConcert = query.value(2).toDate();
    m_tarifHT = query.value(3).toDouble();
    m_TVA = query.value(4).toDouble();
    m_isRepasCompris = query.value(5).toBool();
    m_isHebergementCompris = query.value(6).toBool();
    m_isFraisDeplacementCompris = query.value(7).toBool();
    m_typePrestation = TypePrestation(query, 8);
}

////////////////////////
//
//  Accesseur
//
////////////////////////

void DateObtenu::setClientId(int id)
{
    m_clientId = id;
}
void DateObtenu::setJourConcert(QDate jourConcert)
{
    m_jourConcert = jourConcert;
}
void DateObtenu::setTarifHT(double tarifHT)
{
    m_tarifHT = tarifHT;
}
void DateObtenu::setTVA(double TVA)
{
    m_TVA = TVA;
}
void DateObtenu::setTypePrestation(TypePrestation typePrestation)
{
    m_typePrestation = typePrestation;
}
void DateObtenu::setRepasCompris(bool isRepasCompris)
{
    m_isRepasCompris = isRepasCompris;
}
void DateObtenu::setHebergementCompris(bool isHebergementCompris)
{
    m_isHebergementCompris = isHebergementCompris;
}
void DateObtenu::setFraisDeplacementCompris(bool isFraisDeplacementCompris)
{
    m_isFraisDeplacementCompris = isFraisDeplacementCompris;
}
int DateObtenu::getClientId()const
{
    return m_clientId;
}
QDate DateObtenu::getJourConcert()const
{
    return m_jourConcert;
}
double DateObtenu::getTarifHT()const
{
    return m_tarifHT;
}
double DateObtenu::getTVA()const
{
    return m_TVA;
}
double DateObtenu::getTarifTTC()const
{
    return (m_tarifHT * (1 + m_TVA/100));
}
TypePrestation DateObtenu::getTypePrestation()const
{
    return m_typePrestation;
}
bool DateObtenu::isRepasCompris()const
{
    return m_isRepasCompris;
}
bool DateObtenu::isHebergementCompris()const
{
    return m_isHebergementCompris;
}
bool DateObtenu::isFraisDeplacementCompris()const
{
    return m_isFraisDeplacementCompris;
}
void DateObtenu::BindValues(QSqlQuery &query, QStringList champs)
{
    query.bindValue(":" + champs.at(0), this->getJourConcert().toString("yyyy-MM-dd"));
    query.bindValue(":" + champs.at(1), this->getTarifHT());
    query.bindValue(":" + champs.at(2), this->getTVA());
    int isRepasCompris = (this->isRepasCompris() ? 1 : 0) ;
    int isHebergementCompris = (this->isHebergementCompris() ? 1 : 0) ;
    int isFraisDeplacementCompris = (this->isFraisDeplacementCompris() ? 1 : 0) ;
    query.bindValue(":" + champs.at(3), isRepasCompris);
    query.bindValue(":" + champs.at(4), isHebergementCompris);
    query.bindValue(":" + champs.at(5), isFraisDeplacementCompris);

    query.bindValue(":" + champs.at(6), this->getTypePrestation().getNomPresta());
    query.bindValue(":" + champs.at(7), this->getTypePrestation().getNombreMusiciens());
    query.bindValue(":" + champs.at(8), this->getTypePrestation().getTarifHT());
    int isLumiere = (this->getTypePrestation().isLumiere() ? 1 : 0) ;
    query.bindValue(":" + champs.at(9), isLumiere);
    int isDecorScene = (this->getTypePrestation().isDecorScene() ? 1 : 0) ;
    query.bindValue(":" + champs.at(10), isDecorScene);
    query.bindValue(":" + champs.at(11), this->getTypePrestation().getTypeSono());
    query.bindValue(":" + champs.at(12), this->getTypePrestation().getDivers());

}
////////////////////////
//
//  static SQL
//
////////////////////////
const QString DateObtenu::NomChampClientId = QString("client_id");
const QString DateObtenu::NomChampJourConcert = QString("jourconcert");
const QString DateObtenu::NomChampTarifHT = QString("tarif_ht");
const QString DateObtenu::NomChampTVA = QString("tva");
const QString DateObtenu::NomChampRepasCompris = QString("is_repascompris");
const QString DateObtenu::NomChampHebergementCompris = QString("is_HebergementCompris");
const QString DateObtenu::NomChampFraisDeplacementCompris = QString("is_FraisDeplacementCompris");

QStringList DateObtenu::ListeChampSQL()
{
    QStringList l;
    l << NomChampId << NomChampClientId << NomChampJourConcert << NomChampTarifHT << NomChampTVA
       << NomChampRepasCompris << NomChampHebergementCompris << NomChampFraisDeplacementCompris << TypePrestation::ListeChampSQLSansId();
    return l;
}
QStringList DateObtenu::ListeTypeDataSQL()
{
    QStringList l;
    l << TypeVarId << "SMALLINT UNSIGNED NOT NULL" << "DATE" << "double" << "double"
       << "tinyint" << "tinyint" << "tinyint" << TypePrestation::ListeTypeDataSQLSansId();
    return l;
}
QStringList DateObtenu::SQLWhere()const
{
    QStringList l;
    l <<  NomChampJourConcert << m_jourConcert.toString("yyyy-MM-dd")
        << NomChampTarifHT << QString::number(m_tarifHT)
        << NomChampTVA << QString::number(m_TVA)
        << m_typePrestation.SQLWhere()
        << NomChampRepasCompris << QString::number(m_isRepasCompris)
        << NomChampHebergementCompris << QString::number(m_isHebergementCompris)
        << NomChampFraisDeplacementCompris << QString::number(m_isFraisDeplacementCompris);
    return l;
}
QString DateObtenu::SQLSet()const
{
    int isRepasCompris = (m_isRepasCompris ? 1 : 0);
    int isHebergementCompris = (m_isHebergementCompris ? 1 : 0);
    int isFraisDeplacementCompris = (m_isFraisDeplacementCompris ? 1 : 0);
    return  NomChampJourConcert + "='" + m_jourConcert.toString("yyyy-MM-dd") + "', " + NomChampTarifHT + "='" + QString::number(m_tarifHT) + "', " + NomChampTVA + "='"
            + QString::number(m_TVA)
            + "', " + NomChampRepasCompris + "='" + QString::number(isRepasCompris) + "', " + NomChampHebergementCompris + "='" + QString::number(isHebergementCompris)
            + "', " + NomChampFraisDeplacementCompris + "='" + QString::number(isFraisDeplacementCompris) + "', " + m_typePrestation.SQLSet();  // bug client id en trop ?
}
QString DateObtenu::SetQuery()const
{
//    return NomChampNumeroRue + "=:" + NomChampNumeroRue + ", " + NomChampNomRue + "=:" + NomChampNomRue
//            + ", " +  NomChampCodePostal + "=:" + NomChampCodePostal + ", " + NomChampVille + "=:"
//            + NomChampVille + ", " + NomChampDepartement + "=:" + NomChampDepartement;
}
QStringList DateObtenu::SetBindValues()const
{
    int isRepasCompris = (m_isRepasCompris ? 1 : 0);
    int isHebergementCompris = (m_isHebergementCompris ? 1 : 0);
    int isFraisDeplacementCompris = (m_isFraisDeplacementCompris ? 1 : 0);
    QStringList l;
    l << NomChampJourConcert << m_jourConcert.toString("yyyy-MM-dd")
      << NomChampTarifHT << QString::number(m_tarifHT)
      << NomChampTVA << QString::number(m_TVA)
      << NomChampRepasCompris << QString::number(isRepasCompris)
      << NomChampHebergementCompris << QString::number(isHebergementCompris)
      << NomChampFraisDeplacementCompris << QString::number(isFraisDeplacementCompris)
      << m_typePrestation.SetBindValues()
      << "id" << QString::number(m_id);
    return l;
}
QString DateObtenu::SQLForeignKey()
{
    return ", FOREIGN KEY (" + NomChampClientId + ") REFERENCES clients(id)"; // CONSTRAINT fk_contact_numero
}
////////////////////////
//
//  Operateur
//
////////////////////////
bool DateObtenu::operator==(DateObtenu const& dateObtenu)
{
    return (m_jourConcert == dateObtenu.getJourConcert() &&
            m_tarifHT == dateObtenu.getTarifHT() &&
            m_TVA == dateObtenu.getTVA() &&
            m_typePrestation == dateObtenu.getTypePrestation() &&
            m_isRepasCompris == dateObtenu.isRepasCompris() &&
            m_isHebergementCompris == dateObtenu.isHebergementCompris() &&
            m_isFraisDeplacementCompris == dateObtenu.isFraisDeplacementCompris());
}
bool DateObtenu::isNULL()const
{
    return (m_tarifHT == 0 && m_jourConcert.year() < 2005);
}
